package com.example.demo2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo2.common.Result;
import com.example.demo2.entity.*;
import com.example.demo2.entity.DTO.AddressDTO;
import com.example.demo2.entity.DTO.CustomerDTO;
import com.example.demo2.service.AddressCityService;
import com.example.demo2.service.AddressCountyService;
import com.example.demo2.service.AddressProvinceService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.poi.hssf.usermodel.HeaderFooter.page;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressProvinceService addressProvinceService;
    @Autowired
    private AddressCityService addressCityService;
    @Autowired
    private AddressCountyService addressCountyService;

    //    按省份查询所有的省市区
    @PostMapping("/list")
    public Result findProvince(@RequestBody AddressDTO addressDTO) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        List<AddressProvince> provincelist = addressProvinceService.list(lqw);
        provincelist.forEach(item -> {
            LambdaQueryWrapper<AddressCity> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(AddressCity::getProvinceCode, item.getCode());
            List<AddressCity> cityslist = addressCityService.list(lqw2);
            item.setCitysList(cityslist);
            item.getCitysList().forEach(item2 -> {
                LambdaQueryWrapper<AddressCounty> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(AddressCounty::getCityCode, item2.getCode());
                List<AddressCounty> countyslist = addressCountyService.list(lqw3);
                item2.setCountysList(countyslist);
            });
        });
        map.put("address", provincelist);
        return Result.success(map);
    }

    //    按特定格式查询所有的省
    @GetMapping("/listByProvince")
    public Result listByProvince() {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        List<AddressProvince> provincelist = addressProvinceService.list(lqw);
        provincelist.forEach(item -> {
            item.setValue(item.getCode().toString());
            item.setLabel(item.getName());
        });
        map.put("address", provincelist);
        return Result.success(map);
    }
    //    按特定格式查询所有的省市
    @GetMapping("/listByProvinceAndCity")
    public Result listByProvinceAndCity() {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        List<AddressProvince> provincelist = addressProvinceService.list(lqw);
        provincelist.forEach(item -> {
            item.setValue(item.getCode().toString());
            item.setLabel(item.getName());
            LambdaQueryWrapper<AddressCity> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(AddressCity::getProvinceCode, item.getCode());
            List<AddressCity> cityslist = addressCityService.list(lqw2);
            item.setCitysList(cityslist);
            item.setChildren(cityslist);
            if (item.getChildren().size() == 0) {
                item.setDisabled(true);
            }
            item.getChildren().forEach(item2 -> {
                item2.setValue(item2.getCode().toString());
                item2.setLabel(item2.getName());
            });
        });
        map.put("address", provincelist);
        return Result.success(map);
    }

    //    按特定格式查询所有的省市区
    @GetMapping("/listByProvinceAndCityAndCounty")
    public Result listByProvinceAndCityAndCounty() {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        List<AddressProvince> provincelist = addressProvinceService.list(lqw);
        provincelist.forEach(item -> {
            item.setValue(item.getCode().toString());
            item.setLabel(item.getName());
            LambdaQueryWrapper<AddressCity> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(AddressCity::getProvinceCode, item.getCode());
            List<AddressCity> cityslist = addressCityService.list(lqw2);
            item.setCitysList(cityslist);
            item.setChildren(cityslist);
            if (item.getChildren().size() == 0) {
                item.setDisabled(true);
            }
            item.getChildren().forEach(item2 -> {
                item2.setValue(item2.getCode().toString());
                item2.setLabel(item2.getName());
                LambdaQueryWrapper<AddressCounty> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(AddressCounty::getCityCode, item2.getCode());
                List<AddressCounty> countyslist = addressCountyService.list(lqw3);
                item2.setCountysList(countyslist);
                item2.setChildren(countyslist);
                if (item2.getChildren().size() == 0) {
                    item2.setDisabled(true);
                }
                item2.getChildren().forEach(item3 -> {
                    item3.setValue(item3.getCode().toString());
                    item3.setLabel(item3.getName());
                });
            });

        });
        map.put("address", provincelist);
        return Result.success(map);
    }

    //分页查询所有
    @PostMapping("/findPage")
    public Result findPage(@RequestBody AddressDTO addressDTO) {
        return Result.success(addressProvinceService.findPageOfCustomer(addressDTO));
    }

    //单独查询所有的省
    @GetMapping("/getProvinceList")
    public Result getProvinceList() {
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        List<AddressProvince> provinceList = addressProvinceService.list(lqw);
        provinceList.forEach(item -> {
            item.setValue(item.getCode().toString());
            item.setLabel(item.getName());
        });
        return Result.success(provinceList);
    }

    //单独查询所有的省(分页)
    @PostMapping("/getPageProvinceList")
    public Result getPageProvinceList(@RequestBody AddressDTO addressDTO) {
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(addressDTO.getProvinceName()), AddressProvince::getName, addressDTO.getProvinceName());
        Page<AddressProvince> addressProvincePage = new Page<>(addressDTO.getPage(), addressDTO.getLimit());
        Page<AddressProvince> page = addressProvinceService.page(addressProvincePage, lqw);

        return Result.success(page);
    }

    //单独查询所有的市(分页)
    @PostMapping("/getPageCityList")
    public Result getPageCityList(@RequestBody AddressDTO addressDTO) {
        LambdaQueryWrapper<AddressCity> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(addressDTO.getCityName()), AddressCity::getName, addressDTO.getCityName());
        lqw.like(addressDTO.getProvinceCode() != null, AddressCity::getProvinceCode, addressDTO.getProvinceCode());
        Page<AddressCity> addressCityPage = new Page<>(addressDTO.getPage(), addressDTO.getLimit());
        Page<AddressCity> page = addressCityService.page(addressCityPage, lqw);
        return Result.success(page);
    }

    //单独查询所有的区(分页)
    @PostMapping("/getPageCountyList")
    public Result getPageCountyList(@RequestBody AddressDTO addressDTO) {
        LambdaQueryWrapper<AddressCounty> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(addressDTO.getCountyName()), AddressCounty::getName, addressDTO.getCountyName());
        lqw.like(addressDTO.getCityCode() != null, AddressCounty::getCityCode, addressDTO.getCityCode());
        Page<AddressCounty> addressCountyPage = new Page<>(addressDTO.getPage(), addressDTO.getLimit());
        Page<AddressCounty> page = addressCountyService.page(addressCountyPage, lqw);
        return Result.success(page);
    }

    //根据省份code查询所有的市
    @GetMapping("/getCityList")
    public Result getCityList(@RequestParam Integer provinceCode) {
        LambdaQueryWrapper<AddressCity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressCity::getProvinceCode, provinceCode);
        List<AddressCity> cityList = addressCityService.list(lqw);
        return Result.success(cityList);
    }

    //根据市code查询所有的区
    @GetMapping("/getCountyList")
    public Result getCountyList(@RequestParam Integer cityCode) {
        LambdaQueryWrapper<AddressCounty> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressCounty::getCityCode, cityCode);
        List<AddressCounty> countyList = addressCountyService.list(lqw);
        return Result.success(countyList);
    }

    //添加省
    @PostMapping("/addProvince")
    public Result addProvince(@RequestBody AddressDTO addressDTO) {
        AddressProvince addressProvince = new AddressProvince();
        addressProvince.setCode(addressDTO.getProvinceCode());
        addressProvince.setName(addressDTO.getProvinceName());
        return Result.success(addressProvinceService.save(addressProvince));
    }

    //添加市
    @PostMapping("/addCity")
    public Result addCity(@RequestBody AddressDTO addressDTO) {
        LambdaQueryWrapper<AddressCity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressCity::getCode, addressDTO.getCityCode());
        AddressCity one = addressCityService.getOne(lqw);
        if (one != null) {
            return Result.error("该市编号已存在，请跟换", "-1");
        }
        AddressCity addressCity = new AddressCity();
        addressCity.setProvinceCode(addressDTO.getProvinceCode());
        addressCity.setCode(addressDTO.getCityCode());
        addressCity.setName(addressDTO.getCityName());
        return Result.success(addressCityService.save(addressCity));
    }

    //添加区
    @PostMapping("/addCounty")
    public Result addCounty(@RequestBody AddressDTO addressDTO) {
        LambdaQueryWrapper<AddressCounty> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressCounty::getCode, addressDTO.getCountyCode());
        AddressCounty one = addressCountyService.getOne(lqw);
        if (one != null) {
            return Result.error("该区编号已存在，请跟换", "-1");
        }
        AddressCounty addressCounty = new AddressCounty();
        addressCounty.setCode(addressDTO.getCountyCode());
        addressCounty.setName(addressDTO.getCountyName());
        addressCounty.setCityCode(addressDTO.getCityCode());
        return Result.success(addressCountyService.save(addressCounty));
    }

    // 删除省
    @PostMapping("/deleteProvince")
    public Result deleteProvince(@RequestBody AddressProvince addressProvince) {
        return Result.success(addressProvinceService.removeById(addressProvince.getId()));
    }

    //批量删除省
    @PostMapping("/batch/deleteProvince")
    public Result batchDeleteProvince(@RequestBody List<Integer> ids) {
        return Result.success(addressProvinceService.removeBatchByIds(ids));
    }

    //修改省
    @PostMapping("/updateProvince")
    public Result updateProvince(@RequestBody AddressProvince addressProvince) {
        return Result.success(addressProvinceService.updateById(addressProvince));
    }


    // 删除市
    @PostMapping("/deleteCity")
    public Result deleteCity(@RequestBody AddressCity addressCity) {
        return Result.success(addressCityService.removeById(addressCity.getId()));
    }

    //批量删除市
    @PostMapping("/batch/deleteCity")
    public Result batchDeleteCity(@RequestBody List<Integer> ids) {
        return Result.success(addressCityService.removeBatchByIds(ids));
    }

    //修改市
    @PostMapping("/updateCity")
    public Result updateShop(@RequestBody AddressCity addressCity) {
        return Result.success(addressCityService.updateById(addressCity));
    }


    // 删除区
    @PostMapping("/deleteCounty")
    public Result deleteCounty(@RequestBody AddressCounty addressCounty) {
        return Result.success(addressCountyService.removeById(addressCounty.getId()));
    }

    //批量删除区
    @PostMapping("/batch/deleteCounty")
    public Result batchDeleteCounty(@RequestBody List<Integer> ids) {
        return Result.success(addressCountyService.removeBatchByIds(ids));
    }

    //修改区
    @PostMapping("/updateCounty")
    public Result updateCounty(@RequestBody AddressCounty addressCounty) {
        return Result.success(addressCountyService.updateById(addressCounty));
    }


}
